<?php
/* 
 * Project: oCoder Framework
 * File: /controllers/user.php
 * Purpose: controller for the user of the app.
 * Author: Trung Hoang
 */

class FunctionController extends BaseController
{
    //add to the parent constructor
    public function __construct($action, $urlValues) {
        parent::__construct($action, $urlValues);
         if(!Helper::isLogin()){
            //save current url: after login, auto redirect client to current url
            $_SESSION['lastUrl'] = Helper::currentPageURL();
            //redirect to login page
            header( 'Location: '.BASE_URL.'account/login' ) ;
        }
        //create the model object
        require("models/function.php");
        $this->model = new FunctionModel();      
    }
    
    //default action
    protected function index()
    {
        if(Helper::Authenticate('XemChucNang')){
            if(!empty($_GET['id']))
            {
                 $para = $_GET['id'];
            }
            else $para=1;
            $this->view->output_admin($this->model->index($para)); 
        } else Helper::redirect();
    }
    //create action
    protected function create()
    {
        if(Helper::Authenticate('ThemChucNang')){
            //load data post from client
            if ( !empty($_POST)) {
                $data = array(
                    'TenChucNang'=>$_POST['TenChucNang'],
                    'MoTa'=>$_POST['MoTa']
                );
                $result = $this->model->create($data);
                if($result){
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Thêm chức năng thành công' 
                        );
                    $_SESSION['message'] = $alert;
                    header( 'Location: '. BASE_URL . 'function/index' );
                }
            }
            //load form create
            $this->view->output_admin($this->model->create('')); 
        } else Helper::redirect();
    }
    protected function update(){
        if(Helper::Authenticate('SuaChucNang')){
            //load data update post from client
            if(!empty($_POST)){
                //get data from POST
                $id = $this->urlValues['id'];
                $data = array(
                    'TenChucNang' => $_POST['TenChucNang'], 
                    'MoTa' => $_POST['MoTa']
                    );
                //update item -> return total rows affected
                $result = $this->model->edit($id, $data);
                if($result){
                    $alert = array(
                        'class' => 'alert-success',
                        'message' => 'Chỉnh sửa chức năng thành công' 
                        );
                    $_SESSION['message'] = $alert;
                    header( 'Location: '. BASE_URL . 'function/index' ) ;
                }
            }
            //load form update
            $this->view->output_admin($this->model->edit($id=$this->urlValues['id'], $data=''));
        } else Helper::redirect();
    }

    //delete action
    protected function delete()
    {
        if(Helper::Authenticate('XoaChucNang')){
            // Kiểm tra nút Áp dụng xóa nhiều có được click hay chưa.
           $isDelete = array_key_exists("delete-multi", $_POST);
            //when client delete multi item
            if(!empty($_POST)) {
                if($isDelete==true)
                $result = $this->model->delete('nd_chucnang', 'id IN('.implode(',',$_POST['check_list']).')');
            }
            else
                $result = $this->model->delete('nd_chucnang', 'id='.$this->urlValues['id']);

            if($result){
                $alert = array(
                    'class' => 'alert-success',
                    'message' => 'Xóa chức năng thành công' 
                    );
                $_SESSION['message'] = $alert;
            }

            header( 'Location: '. BASE_URL . 'function/index' );
        } else Helper::redirect();
    }
    protected function getFunction(){
         if(array_key_exists('delete-multi', $_POST))
         {
            if($_POST['action']=="delete"){
            $this->alert('alert-warning','Chưa chọn tác vụ để xóa');   
            return;
            }
            if(isset($_POST['check_list']))
             $this->delete();
             else {
                $this->alert('alert-warning','Chưa chọn chức năng để xóa');
                return;
            }
         }
        else if(array_key_exists('btnsearch', $_POST))
             $this->findUser();
    }
    protected function findUser(){
        echo 'thuc hien tim kiem nguoi dung';
        die();
    }
    function alert($class,$message){
        $alert = array(
                    'class' => $class,
                    'message' => $message 
                    );
                $_SESSION['message'] = $alert;
                header( 'Location: '. BASE_URL . 'function/index');
    }
}

?>